<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;

class question extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1,$class = '*')
    {
        Cookie::get('index_name')?Session::set('index_name',Cookie::get('index_name')):'';
        Cookie::get('index_id')?Session::set('index_id',Cookie::get('index_id')):'';
        $arr = array();
        if ($class == '*') {
            $className = '问题广场';
        }else{
            $className = Db::name('category c')->where('c.id='.$class)->find()['name'];
            $arr['c.id'] = $class;
        }
        if (empty(request()->post())){
            $count = Db::name('question q,gqx_category c')->where($arr)->where('c.display=1 and c.id=q.cid ')->count('q.id');
            // return $count;
            $count = ceil($count/10);
            //判断1<=$page<max
            if ($page < 1){
                $page = 1;
            }elseif ($page > $count){
                $page = $count;
            }
            $data = Db::name('question q')->join('gqx_user u','u.id=q.uid')->join('gqx_category c','q.cid=c.id')->join('gqx_user_info i','i.id=u.id')->field('c.*,c.id as cateid,q.*,u.name as uname,i.photo')->where($arr)->where('c.display=1 and c.id=q.cid ')->page($page,10)->order(['likes'=>'desc'])->select();
        }else{
            $post = request()->post()['s'];
            $count = Db::name('question')->where('title','like','%'.$post.'%')->count('id');
            // return $count;
            $count = ceil($count/10);
            //判断1<=$page<max
            if ($page < 1){
                $page = 1;
            }elseif ($page > $count){
                $page = $count;
            }
            $data = Db::name('question q')->join('gqx_user u','u.id=q.uid')->join('gqx_category c','q.cid=c.id')->join('gqx_user_info i','i.id=u.id')->field('c.*,c.id as cateid,q.*,u.name as uname,i.photo')->where('q.title','like','%'.$post.'%')->where('c.display=1 and c.id=q.cid ')->page($page,10)->order(['likes'=>'desc'])->select();
        }
        $namelist = Db::name('category c')->where('c.display=1')->select();
        return view('question/index',['CSS_PATH'=>config('index_css'),
            'JS_PATH'=>config('index_js'),
            'ICON_PATH'=>config('index_icon'),
            'IMG_PATH'=>config('index_img'),
            'index_name'=>Session::get('index_name'),
            'index_id'=>Session::get('index_id'),
            'namelist'=>$namelist,
            'className'=>$className,
            'count'=>$count,
            'page'=>$page,
            'data'=>$data
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存提出的问题
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if (empty(Session::get('index_name'))){
            return $this->redirect('index/login/index');
        }
        $post = $request->post();
        $data['time'] = date('Y-m-d H:i:s');
        $data['uid'] = Session::get('index_id');
        $data['title'] = $post['title'];
        $data['cid'] = $post['cid'];
        $data['content'] = $post['editorValue'];
        $result = Db::name('question')->insert($data);
        if ($result){
            return $this->redirect('index/index/index', ['cate_id' => 2]);
        }
    }

    /**
     * 保存回复
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function reply(Request $request)
    {
        if (empty(Session::get('index_id'))){
            return $this->redirect('index/login/index');
        }
        // return var_dump($request->param());
        $data['qid'] = $request->param()['qid'];
        $data['content'] = $request->param()['content'];
        @empty($request->param()['aid']) ? : $data['aid']=$request->param()['aid'];
        // 先写死ID
        $data['uid'] = 1;
        // return var_dump($data);
        $res = Db::name($request->param()['motion'])->insert($data);
        return $res;

    }

    /**
     * 点赞
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function likes(Request $request)
    {
        if (empty(Session::get('index_id'))){
            return $this->redirect('index/login/index');
        }
        @empty($request->param()['qid']) ? : $id = $data['qid'] = $request->param()['qid'];
        @empty($request->param()['aid']) ? : $id = $data['aid'] = $request->param()['aid'];

        // ID
        $data['uid'] = 1;
        $check = Db::name($request->param()['motion'].'_likes')->where($data)->count();
        // return var_dump($check);
        if($check > 0){
            return "已存在";
        }

        // 启动事务
        Db::startTrans();
        try{
            Db::name($request->param()['motion'].'_likes')->insert($data);
            Db::name($request->param()['motion'])->where('id='.$id)->setInc('likes');
            // 提交事务
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 1213;
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $data = Db::name('question q, gqx_user u,gqx_user_info i')->field('q.*,u.name,i.photo')->where('q.id='.$id.' and u.id=q.uid and i.id=u.id')->find();
        $answer = Db::name('answer a, gqx_user u,gqx_user_info i')->field('a.id as aid,a.*,u.name,u.id as uid,i.photo')->where('a.qid='.$data['id'].' and u.id=a.uid and a.status=1 and i.id=u.id')->order(['a.id'=>'asc'])->select();
        $replys = array();
        foreach ($answer as $v) {
            $replys[$v['aid']] = Db::name('reply r, gqx_user u')->field('r.uid,r.content,r.time,u.name')->where('r.aid='.$v['aid'].' and r.status=1 and u.id=r.uid')->order(['r.id'=>'asc'])->select();
        }
        $namelist = Db::name('category c')->where('c.display=1')->select();
        // return var_dump($data,$answer,$replys);
        // 
        Cookie::get('index_id')?Session::set('index_id',Cookie::get('index_id')):'';

        return view('question/info2',['CSS_PATH'=>config('index_css'),
            'JS_PATH'=>config('index_js'),
            'ICON_PATH'=>config('index_icon'),
            'IMG_PATH'=>config('index_img'),
            'index_name'=>Session::get('index_name'),
            'index_id'=>Session::get('index_id'),
            'data'=>$data,
            'answer'=>$answer,
            'namelist'=>$namelist,
            'replys'=>$replys
        ]);
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
